package com.FindMyStuff.Model;


/**
 * Represents a User, keeps track of email and password
 *
 */

public class User {
		/**
		 * User's email
		 */
		private String email;
		/**
		 * User's password
		 */
		private String password;
		/**
		 * Role the user was granted
		 */
		private UserRole role;
		/**
		 * Login attempts left the user has left
		 */
		public int loginAttemptsLeft;
		/**
		 * If the user is locked or not
		 */
		private UserStatus status; 
		
		/**
		 * Creates a user with the given email
		 * and password
		 * 
		 * @param email User's email
		 * @param password User's password
		 */
		
		public User(String email, String password, UserRole role)
		{
			this.email = email;
			this.password = password;
			this.role = role;
			loginAttemptsLeft = 3;
			status = UserStatus.Unlocked;
		}
		
		/**
		 * Creates a user with the given email
		 * and password
		 * 
		 * @param String User's email
		 * @param String User's password
		 * @param UserRole User's role
		 * @param UserStatus User's status
		 * @param int Remaining Attempts
		 */
		public User(String email, String password, UserRole role, UserStatus status, int loginAttemptsLeft)
		{
			this.email = email;
			this.password = password;
			this.role = role;
			this.loginAttemptsLeft = loginAttemptsLeft;
			this.status = status;
		}
		
		/**
		 * A getter for email
		 * 
		 * @return email
		 */
		
		public String getEmail()
		{
			return email;
		}
		
		/**
		 * Checks the given password against the
		 * actual password
		 * 
		 * @param password The password to check
		 * @return true if given password is correct, else false
		 */
		
		public boolean checkPassword(String password)
		{
			return this.password.equals(password);
		}
		
		/**
		 * Checks if the user is locked or not
		 * @return boolean true if user is locked
		 */
		public boolean isLocked()
		{
			return status.toBool();
		}
		
		/**
		 * Checks if the user's role is an Admin
		 * @return boolean true if user is Admin
		 */
		public boolean isAdmin()
		{
			return (role == UserRole.Admin);
		}
		
		/**
		 * Subtracts from the attempts the User as left
		 * @return boolean true if the user is out of attempts
		 */
		public boolean logInFailed()
		{
			loginAttemptsLeft--;
			if (loginAttemptsLeft == 0)
			{
				status = UserStatus.Locked;
				return true;
			}
			return false;
		}
		
		/**
		 * Sets attempts to 3
		 */
		public void logInSucess()
		{
			loginAttemptsLeft = 3;
		}
		
		/**
		 * locks the user if the calling user is an admin
		 * @param User user to lock
		 */
		public void lockUser(User user)
		{
			if(role == UserRole.Admin)
			{
				user.status = UserStatus.Locked;
				user.loginAttemptsLeft = 0;
			}
		}
		
		/**
		 * unlocks the user if the calling user is an admin
		 * @param User user to unlock
		 */
		public void unlockUser(User user)
		{
			if(role == UserRole.Admin)
			{
				user.status = UserStatus.Unlocked;;
				user.loginAttemptsLeft = 3;
			}
		}
		
		/**
		 * Check if the user is actually a user or not
		 * @return true if the user is a user
		 */
		public boolean exist() {
			return true;
		}
		
		public String toString()
		{
			return "\nUser: email: " + email + " lock status: " + status.toBool() + "attempts left: " + loginAttemptsLeft;
		}
}
